<?php
class ControllerCatalogProductGroup extends AdminController {
	public function index() {
		M("catalog/product_group");
		$this->getList();
	}
	
	public function update() {
		M("catalog/product_group");
		
		if ($this->request->isPost()) {
			$this->model_catalog_product_group->editGroup($this->request->get['product_group_id'], $this->request->post);
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/product_group'));
		}
		$this->getForm();
	}
	
	public function insert() {
		M("catalog/product_group");
	
		if ($this->request->isPost()) {
			$this->model_catalog_product_group->addGroup($this->request->post);
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/product_group'));
		}
		$this->getForm();
	}
	
	
	public function delete() {
		if ($this->request->isPost() && !empty($this->request->post['selected'])) {
			M("catalog/product_group");
			foreach($this->request->post['selected'] as $product_group_id) {
				$this->model_catalog_product_group->deleteGroup($product_group_id);
			}
			$this->session->set_flashdata('success', $this->_['text_success']);
		}
		$this->redirect(UA("catalog/product_group"));
	}
	
	protected function  getList() {
		$this->document->setTitle($this->_['heading_title']);
		
		$page = G('page', 1);
		$start = ($page - 1) * C('config_admin_limit');
		$limit= C('config_admin_limit');
		
		$this->data['product_groups'] = $this->model_catalog_product_group->getGroups($start, $limit, $total);
		
		$this->data['pagination'] = $this->pagination($total, null, null, $page)->render();
		
		$this->display('catalog/product_group_list.tpl');
	}
	
	protected  function getForm() {
		$this->document->addScript(ROOT . 'static/javascript/jquery/jquery.validate.js');
			
		$product_group_id = G('product_group_id', 0);
		
		$this->document->setTitle($this->_['heading_title']);
	
		if ($product_group_id) {
			$this->data['action'] = UA('catalog/product_group/update', 'product_group_id=' . $product_group_id);
			$this->vo->data = $this->model_catalog_product_group->getGroup($product_group_id);
		}
		else {
			$this->data['action'] = UA('catalog/product_group/insert');
		}
	
		$this->display('catalog/product_group_form.tpl');
	}
	
	public function product() {
		$this->document->setTitle($this->_['heading_title']);
		
		$product_group_id = G('product_group_id');
		M("catalog/product_group");
		$this->data['products'] = $this->model_catalog_product_group->getGroupProducts($product_group_id);
		$this->data['product_group_id'] = $product_group_id;
		
		$this->display('catalog/product_group_product.tpl');
	}
	
	public function product_delete() {
		$product_group_id = G('product_group_id');
		if ($this->request->isPost()&& $this->hasMP()) {
			$product_ids = P('selected');
			if ($product_ids) {
				$product_ids = implode(',', $product_ids);
				$this->db->runSql("DELETE FROM @@product_group_products WHERE product_group_id = $product_group_id AND product_id IN ($product_ids)");
			}
		}
		$this->redirect(UA("catalog/product_group/product", 'product_group_id=' . $product_group_id));
	}
	
	public function product_add() {
		$product_group_id = G('product_group_id');
		if ($this->request->isPost() && $this->hasMP()) {
			$product_ids = P('product_ids');
			if ($product_ids) {
				M("catalog/product_group");
				$this->model_catalog_product_group->setGroupProducts($product_group_id, $product_ids);
			}
		}		
		$this->redirect(UA("catalog/product_group/product", 'product_group_id=' . $product_group_id));
	}
}
?>